package com.skydonkey.dao;

import com.github.pagehelper.Page;
import com.skydonkey.pojo.po.VideoInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface VideoInfoMapper {

    @Select("SELECT * FROM video_info WHERE uid = #{uid}")
    Page<VideoInfo> findByUid(@Param("uid") Long uid);

    @Select("SELECT * FROM video_info WHERE video_id = #{videoId}")
    VideoInfo findByVideoId(@Param("videoId") Long videoId);

    @Insert("INSERT INTO video_info (uid, video_key, video_url, cover_key, cover_url, title, permission, publish_now, join_video_act, `desc`, duration) " +
            "VALUES (#{uid}, #{videoKey}, #{videoUrl}, #{coverKey}, #{coverUrl}, #{title}, #{permission}, #{publishNow}, #{joinVideoAct}, #{desc}, #{duration})")
    @Options(useGeneratedKeys = true, keyProperty = "videoId", keyColumn = "video_id")
    int insert(VideoInfo videoInfo);


    @Update("UPDATE video_info " +
            "SET video_id = #{videoId}, video_key = #{videoKey}, video_url = #{videoUrl}, " +
            "cover_key = #{coverKey}, cover_url = #{coverUrl}, title = #{title}, permission = #{permission}, " +
            "publish_now = #{publishNow}, join_video_act = #{joinVideoAct}, create_time = #{createTime}, " +
            "update_time = #{updateTime}, is_delete = #{isDelete}, desc = #{desc} " +
            "WHERE uid = #{uid}")
    void update(VideoInfo videoInfo);

    @Delete("DELETE FROM video_info WHERE uid = #{uid}")
    void deleteByUid(@Param("uid") Long uid);

    @Select("SELECT * FROM video_info ")
    Page<VideoInfo> findAll();

    @Select("SELECT * FROM video_info where uid = #{uid}")
    Page<VideoInfo> findAllByUid(Long uid);

    @Select("SELECT v.* FROM video_info AS v " +
            "INNER JOIN video_category AS vc " +
            "ON v.video_id = vc.video_id " +
            "WHERE vc.category_id = #{categoryId} and v.permission = 0")
    Page<VideoInfo> findByCategoryId(Integer categoryId);

    @Select("SELECT " +
            "    vi.uid, " +
            "    vi.video_id, " +
            "    vi.video_key, " +
            "    vi.video_url, " +
            "    vi.cover_key, " +
            "    vi.cover_url, " +
            "    vi.title, " +
            "    vi.permission, " +
            "    vi.publish_now, " +
            "    vi.join_video_act, " +
            "    vi.create_time, " +
            "    vi.update_time, " +
            "    vi.duration, " +
            "    is_delete, " +
            "    COUNT(vml.record_id) AS like_count " +
            "FROM video_info AS vi " +
            "LEFT JOIN videos_my_likes AS vml ON vi.video_id = vml.video_id " +
            "GROUP BY vi.video_id " +
            "ORDER BY like_count DESC ")
    Page<VideoInfo> getMostLikedVideos();

    @Select("SELECT * " +
            "FROM video_info " +
            "order by create_time desc ")
    Page<VideoInfo> getLatestPublishedVideos();


    @Update("UPDATE video_info " +
            "set permission = #{permission} " +
            "where video_id = #{videoId} ")
    int updatePermission(String permission, Long videoId);

    @Delete("delete from video_info where video_id = #{videoId} ")
    int deleteVideoInfoMapper(Long videoId);


    //    @Select("SELECT video_id FROM video_info " +
//            "WHERE MATCH(title) AGAINST(CONCAT('+', #{key}, '') IN BOOLEAN MODE)")
    @Select("SELECT video_id FROM video_info " +
            "WHERE title like CONCAT('%', #{key}, '%') order by create_time desc ")
    Page<Long> getVideoIdByTitle(String key);


}
